package com.collins.reflections;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class PrintFields {

    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        Student student = new Student();
        try {
            print(teacher);
            print(student);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void print(Object object) throws FileNotFoundException {
        PrintStream ps = null;
        try {
            ps = new PrintStream(
                    new FileOutputStream("files/prints/print.txt", true), true);
            Class<?> c = object.getClass();
            String cName = c.getSimpleName();
            for (Field field : c.getDeclaredFields()) {
                field.setAccessible(true);
                ps.println(cName + ": " + field.getName());
            }
        } catch (FileNotFoundException | SecurityException e) {
            throw new RuntimeException(e);
        } finally {
            if (ps != null) {
                ps.close();
            }
        }

    }
}
